//
//  ChargeDetailVC.m
//  ImGuider
//
//  Created by 李陆涛 on 2018/1/3.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "ChargeDetailVC.h"
#import "ChargeDetailCell.h"

@interface ChargeDetailVC ()

@property (nonatomic, strong) NSArray *monthArray;
@property (nonatomic, strong) NSDictionary *dataSourceDict;

//@property (nonatomic) NSInteger start;

@end

@implementation ChargeDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = MYLocalizedString(@"COIN_TOPUP_RECORD", nil);
    
    self.tableView.tableFooterView = [UIView new];
    [self registCells:@[@"ChargeDetailCell"]];
    
    @weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
//        self.start = 0;
        [self loadDataSource];
    }];
    
//    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        @strongify(self);
//        [self loadDataSource];
//    }];
    
    [self.tableView setThemeStyle:ThemeStyleBlack];
    [ActivityIndicatorView showHUDAddedTo:self.view];
    [self loadDataSource];
}

//- (NSMutableDictionary *)dataSourceDict {
//
//    if (!_dataSourceDict) {
//        _dataSourceDict = [NSMutableDictionary dictionary];
//    }
//    return _dataSourceDict;
//}
//
//- (NSMutableArray *)monthArray {
//
//    if (!_monthArray) {
//        _monthArray = [NSMutableArray array];
//    }
//    return _monthArray;
//}

- (void)loadDataSource {
    
    [NetAPIManager getExpenseCalendarListWithPageno:0 pagesize:1000 Block:^(NSArray<ExpenseModel *> *expenselistArray, NSError *error) {
        
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        [self.tableView.mj_header endRefreshing];
        
//        if (self.start == 0) {
//            [self.tableView.mj_footer resetNoMoreData];
//            [self.monthArray removeAllObjects];
//            [self.dataSourceDict removeAllObjects];
//        }
//
//        if (expenselistArray.count < 20) {
//            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//        } else {
//
//            [self.tableView.mj_footer endRefreshing];
//        }
        
        NSMutableArray *monthArray = [NSMutableArray array];
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        
        for (ExpenseModel *model in expenselistArray) {
            
            NSString *dateStr = [[model.consumetime componentsSeparatedByString:@" "] firstObject];
            
            NSArray *dateArray = [dateStr componentsSeparatedByString:@"-"];
            
            if (dateArray.count != 3) {
                
                continue;
            }
            
            dateStr = [NSString stringWithFormat:@"%@年%@月",dateArray[0],dateArray[1]];
            
            NSMutableArray *listArray = dict[dateStr];
            
            if (!listArray) {
                listArray = [NSMutableArray array];
                [dict setObject:listArray forKey:dateStr];
                [monthArray addObject:dateStr];
            }
            
            [listArray addObject:model];
        }
        
        self.monthArray = monthArray;
        self.dataSourceDict = dict;
        
        [self.tableView reloadData];
        
//        self.start ++;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.monthArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSArray *listArray = self.dataSourceDict[self.monthArray[section]];
    
    return listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray *listArray = self.dataSourceDict[self.monthArray[indexPath.section]];
    
    ExpenseModel *model = listArray[indexPath.row];
    
    ChargeDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChargeDetailCell"];
    
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headView = [UIView new];
    
    headView.backgroundColor = kBGColor;
    
    UILabel *monthLabel = [UILabel new];
    monthLabel.textColor = kContentColor;
    monthLabel.font = Font(12);
    [monthLabel sizeToFit];
    
    [headView addSubview:monthLabel];
    
    monthLabel.text = self.monthArray[section];
    
    [monthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(headView.mas_centerY);
        make.left.equalTo(headView.mas_left).offset(16);
    }];
    
    
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 30;
}

@end
